package com.processor.base;

import com.event.ProcessorEvent;
import com.handler.game.BaseHandler;
import com.handler.base.ThreadProcessorHandler;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author : gameZhao
 * @version V1.0
 * @Project: disruptorDemo
 * @Package com.processor.base
 * @Description: TODO
 * @date Date : 2021年12月28日 13:43
 */
public abstract class ThreadProcessor extends BaseProcessor implements Runnable {
    
    private boolean stop;
    
    /**
     * 线程队列
     **/
    protected final BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>();
    
    
    public ThreadProcessor(String processorName) {
        this.processorName = processorName;
        new Thread(this).start();
    }
    
    @Override
    public void executeHandler0(BaseHandler handler) {
        try {
            handler.setTime(System.currentTimeMillis());
            ThreadProcessorHandler threadPoolProcessorHandler = new ThreadProcessorHandler(new ProcessorEvent(handler));
            workQueue.put(threadPoolProcessorHandler);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public void run() {
        
        while (!stop) {
            try {
                var r = workQueue.take();
                r.run();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
}
